package app

import (
	"chatgpt/model/base"
	"github.com/slclub/go-tips"
)

type Order struct {
	OrderID uint   `gorm:"primaryKey;column:order_id;autoIncrement"`
	OrderNo string `gorm:"size:128;index;comment:订单编号"`
	PayNo   string `gorm:"size:128;index;comment:支付编号"`
	PayType string `gorm:"size:128;index;comment:支付方式"`
	ProcID  int    `gorm:"type:int(10);column:proc_id;comment:商品ID"`
	base.UUIDR
	Num      int    `gorm:"type:int(10);comment:数量"`
	Amount   int    `gorm:"type:int(10);comment:金额"`
	State    int    `gorm:"type:int(4);comment: 1下单，2无效，4支付成功 8 已发货"`
	Finish   int    `gorm:"type:int(4);comment:1进行中，2已关闭，3已完成"`
	CallBack string `gorm:"size:255;comment:回调完成的进度数据"`
	base.ModelTime
	Product *Product `gorm:"-"`
}

func (self *Order) TableName() string {
	return "gpt_order"
}

func (self *Order) ConvFromMap(m map[string]any) {
	if val, ok := m["OrderID"]; ok && self.OrderID == 0 {
		self.OrderID = uint(tips.Any2Int64(val))
	}
	if val, ok := m["OrderNo"]; ok {
		self.OrderNo = tips.String(val)
	}
	if val, ok := m["PayNo"]; ok {
		self.PayNo = tips.String(val)
	}
	if val, ok := m["PayType"]; ok {
		self.PayType = tips.String(val)
	}
	if val, ok := m["ProcID"]; ok {
		self.ProcID = tips.Int(val)
	}
	self.UUIDR.ConvFromMap(m)
	if val, ok := m["Num"]; ok {
		self.Num = tips.Int(val)
	}
	if val, ok := m["Amount"]; ok {
		self.Amount = tips.Int(val)
	}
	if val, ok := m["State"]; ok {
		self.State = tips.Int(val)
	}
	if val, ok := m["Finish"]; ok {
		self.Finish = tips.Int(val)
	}
	if val, ok := m["CallBack"]; ok {
		self.CallBack = tips.String(val)
	}
	self.ModelTime.ConvToMap(m)
}

func (self *Order) ConvToMap() map[string]any {
	rtn := map[string]any{
		"OrderID":  self.OrderID,
		"OrderNo":  self.OrderNo,
		"PayNo":    self.PayNo,
		"PayType":  self.PayType,
		"ProcID":   self.ProcID,
		"UUID":     self.UUID,
		"Num":      self.Num,
		"Amount":   self.Amount,
		"State":    self.State,
		"Finish":   self.Finish,
		"CallBack": self.CallBack,
	}
	self.ModelTime.ConvToMap(rtn)
	return rtn
}
